<template>
    <!--订单查询-->
    <div class="content_panel project_modules">
        <div class="content_panel_top">
            <h3 class="panel_title">
                <span>{{ $t('common.platformData') }}</span>
            </h3>
            <el-form v-if="isShowSearch" ref="form" :model="form" :rules="rules" :inline="false" class="module_form">
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item prop="date" label="">
                            <el-date-picker
                                    style="width:100%"
                                    v-model="form.date"
                                    type="datetimerange"
                                    format="yyyy-MM-dd HH:mm"
                                    value-format="timestamp"
                                    :editable="false"
                                    range-separator="-"
                                    :default-time="['00:00:00', '23:59:59']"
                                    :start-placeholder="$t('common.startDate')"
                                    :end-placeholder="$t('common.endDate')"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4" v-if="skillTypeId == '100'">
                        <el-form-item label="">
                            <el-form-item><!--公司-->
                                <el-select class="account_form_type" v-model="form.company" :placeholder="$t('system.companyName')" clearable style="width: 100%">
                                    <el-option v-for="company in companyList" :key="company.id" :value="company.name" :label="company.name"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4" v-if="skillTypeId == '100'">
                        <el-form-item prop="domain"><!--公司账号-->
                            <el-input v-model="form.domain"  clearable :placeholder="$t('system.companyAccount')" style="width:100%"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-button @click="search" type="primary">{{ $t('common.search') }}</el-button>
                        <el-button @click="beforeExportExcel" type="primary">{{ $t('common.export') }}</el-button>
                    </el-col>
                </el-row>
            </el-form>
        </div><!--content_panel_top-->
        <div class="module_content">
            <div class="content_panel project_modules">
                <div class="table_list">
                    <el-table ref="table" :data="tableData" style="width: 100%;" :max-height="tableMaxHeight" :highlight-current-row="true">
                        <el-table-column prop="statisticsDate" :label="$t('common.date')"><!--日期--></el-table-column>
                        <el-table-column prop="registerCount" :label="$t('common.registerNumber')"></el-table-column><!--注册数-->
                        <el-table-column prop="orderCount" :label="$t('common.applyNumber')"></el-table-column><!--申请数-->
                        <el-table-column prop="auditSuccessCount" :label="$t('common.passAuditNumber')"></el-table-column><!--信审通过数-->
                        <el-table-column prop="loanSuccessCount" :label="$t('common.loanSuccessNumber')"></el-table-column><!--放款成功数-->
                        <el-table-column prop="contractCountAmount" :label="$t('common.contractTotalAmount')">
                            <template slot-scope="{ row }">{{ row.contractCountAmount | formatCurrency }}</template>
                        </el-table-column><!--合同总金额-->
                    </el-table>
                    <pagination @pageChange="pageChange" :page="page"></pagination>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import mixin from '@/service/mixin';
    import Pagination from '@/components/pagination';
    import ElRow from "element-ui/packages/row/src/row";

    let that = null;
    export default {
        name: 'report',
        components: {
            ElRow,
            Pagination
        },
        mixins: [mixin],
        data() {
            return {
                form: {
                    date: '',
                    company: '',
                    domain: ''
                },
                rules: {},
                companyList: [],
                skillTypeId: this.$storage.get('user').skillTypeId
            };
        },
        computed: {
            isShowSearch() {
                return true;
            }
        },
        methods: {
            // 获取列表数据
            getTableData(page = this.page,callBack) {
                const params = this.fillParams(page);
                this.$api.report.queryuserdatacompanylist(params).then(res => {
                    const {status, data, error} = res;
                    if ('1' === status && '00000000' === error && data) {
                        if (callBack) {
                            if (data.page.totalRecord <= 30000) {
                                callBack();
                                return;
                            }else {
                                this.$message.warning(this.$t('common.exportTips'));
                                return;
                            }
                        }
                        this.tableData = data.resultData || [];
                        this.setPage(data.page, this.page);
                    }
                    this.tableMaxHeight = document.body.clientHeight - 200;
                });
            },
            // 搜索列表数据
            search() {
                if (!this.form.date) {
                    this.$message.warning(this.$t('verify.plsChooseDate'));
                    return;
                }
                this.page.currentPage = 1;
                this.getTableData();
            },
            // 配置查询和导出参数
            fillParams(page = this.page) {
                const form = this.form;
                const params = {
                    company: form.company,
                    beginDate: '',
                    endDate: '',
                    domain: '',
                    page: {
                        pageNo: page.currentPage,
                        pageSize: this.page.pageSize
                    }
                };
                Object.keys(form).forEach(key => {
                    if (typeof form[key] === 'string' && form[key]) {
                        form[key] = form[key].trim();
                        params[key] = form[key];
                    }
                });
                if (form.date) {
                    params.beginDate = form.date[0];
                    params.endDate = form.date[1];
                }
                return params;
            },
            getCompanyList() {
                const params = {
                    userName: '',
                    id: '',
                    status: 1,
                    pageNum: 1,
                    pageSize: 100
                };
                this.$api.systemManage.getCompanyList(params).then((res) => {
                    const {status, data, error} = res;
                    if ('1' === status && error === '00000000') {
                        this.companyList = data.companyList || [];
                    }
                });
            },
            beforeExportExcel() {
                if (!this.form.date) {
                    this.$message.warning(this.$t('verify.plsChooseDate'));
                    return;
                }
                this.getTableData(this.page, this.exportExcel)
            },
            exportExcel() {
                let params = this.fillParams();
                const user = this.$storage.get("user");
                const language = this.$storage.getLocal('language');
                params.language = language;
                params.token = user.token;
                const {$apiConf} = this;
                const url = this.$urlUtil.fillParams(params, $apiConf.baseUrl + $apiConf.urlType.report.exprotuserdatacompany, ["page"]);
                console.log(url);

                this.$openNewBlank(url);
            },
        },
        created() {
            that = this;
            // this.getTableData();
            if (this.skillTypeId == '100') {
                this.getCompanyList();
            }
        },
    };
</script>

<style lang="scss">
    [data-page=report] {
        .noteRed {
            color: red;
        }
        .el-col {
            padding-left: 10px;
        }
        .el-table .cell, .el-table th > .cell {
            word-break: keep-all;
            word-wrap: break-word;
        }
        .el-tooltip__popper {
            max-width: 300px;
            word-break: break-all;
            background-color: #fff;
            box-shadow: 0px 0px 5px #ccc;
            color: #000;
        }
        .el-date-editor .el-range-separator {
            width: 35px;
        }
    }
</style>
